<template>
  <div>
    <div class="margin-container-list">
      <!--顶部搜索 -->
      <div class="top-operate">
        <div>
          <div class="float-l">
            <el-input v-model.trim="searchListQuery.title" placeholder="商品名称" style="width: 200px"
              @keyup.enter.native="handleSearch"></el-input>
            <el-cascader v-model="categoryIds" placeholder="商品零售分类" :props="{ checkStrictly: true }"
              :options="categoryData" filterable clearable style="width: 250px"></el-cascader>
            <el-select v-model="searchListQuery.brandId" placeholder="商品品牌" clearable style="width: 120px">
              <el-option v-for="item in listBrands" :key="item.brandId" :label="item.brandName" :value="item.brandId">
              </el-option>
            </el-select>
            <el-select v-model="searchListQuery.goodsType" placeholder="商品类型" clearable style="width: 150px">
              <el-option label="零售商品" value="30"></el-option>
              <el-option label="批发商品" value="31"></el-option>
              <el-option label="零售+批发商品" value="32"></el-option>
            </el-select>
            <el-select v-model="searchListQuery.goodsStatus" placeholder="上下架状态" clearable style="width: 150px">
              <el-option label="上架" value="1"></el-option>
              <el-option label="下架" value="0"></el-option>
            </el-select>
            <el-button-group class="mar-l-20">
              <el-button type="primary" @click="handleSearch">查询</el-button>
              <el-button plain @click="resetSearch">重置</el-button>
            </el-button-group>
          </div>
          <div class="btn-search">
            <br>
            <el-button type="primary" @click="doAddGoods('')">批量添加</el-button>
          </div>
        </div>
      </div>

      <!-- 内容列表 -->
      <div class="table-container">
        <el-table :data="dataList" v-loading="listLoading" @selection-change="handleSelectionChange"
          element-loading-text="加载中">
          <el-table-column width="50px" type="selection"></el-table-column>
          <el-table-column min-width="320px" label="商品名称" prop="title">
            <template slot-scope="scope">
              <div class="v-align-c cursor-p">
                <label><img :src="scope.row.mainImage" class="user-avatar"></label>
                <div class="pad-l-10">
                  <div class="line-2 width-200">
                    <span class="font-blue" :title="scope.row.title">{{ scope.row.title }}</span>
                  </div>
                  <div>
                    <span class="font-color-6 font-13">商品货号：{{scope.row.goodsCode}}</span>
                  </div>
                </div>
              </div>
            </template>
          </el-table-column>
          <el-table-column width="100px" label="上下架状态" prop="goodsStatus">
            <template slot-scope="scope">
              <span v-if="scope.row.goodsType === 30">
                <span>{{dict.goodsStatus[scope.row.goodsStatus]}}</span>
              </span>
              <span v-else-if="scope.row.goodsType === 31">
                <span>{{dict.goodsStatus[scope.row.wholesaleGoodsStatus]}}</span>
              </span>
              <span v-else-if="scope.row.goodsType === 32">
                <span>{{dict.goodsStatus[scope.row.wholesaleGoodsStatus || scope.row.goodsStatus]}}</span>
              </span>
            </template>
          </el-table-column>
          <el-table-column width="120px" label="商品类型" prop="goodsType">
            <template slot-scope="scope">
              {{dict.goodsType[scope.row.goodsType]}}
            </template>
          </el-table-column>
          <el-table-column width="150px" label="操作" align="center">
            <template slot-scope="scope">
              <el-button type="text" size="medium" @click="doAddGoods(scope.row)">添加</el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <!-- 列表分页 -->
      <div class="pagination">
        <el-pagination background @size-change="handleSizeChange" @current-change="handleCurrentChange"
          :current-page="searchListQuery.currentPage" :page-sizes="[10, 20, 50, 100]" :page-size="searchListQuery.size"
          layout="total, sizes, prev, pager, next, jumper" :total="total">
        </el-pagination>
      </div>
    </div>
    <div class="dialog-footer mar-b-20">
      <el-button type="primary" @click="doCancel">关闭</el-button>
    </div>
  </div>
</template>

<script>
  import {
    listBrand
  } from '@/api/user/enterpriseBrand'
  import {
    pageWarehouseSelectGoods,
    listGoodsCategory
  } from '@/api/goods/goods'
  import {
    getDictByGroupName
  } from '@/api/common/common'
  import {
    addGoods
  } from '@/api/warehouse/warehouseGoods'

  export default {
    props: {
      showFlag: {
        type: Boolean,
        default() {
          return false
        }
      },
      // 仓库id
      warehouseId: {
        type: String
      },
      // 商品来源（排除供货联盟商品用）
      goodsSource: {
        type: Number
      }
    },
    data() {
      return {
        // 查询条件
        searchListQuery: {
          enterpriseId: '',
          supplierId: '',
          currentPage: 1,
          size: 10,
          title: '',
          brandId: '',
          goodsType: '',
          goodsStatus: ''
        },
        // 分类model
        categoryIds: [],
        // 数据字典
        dict: {
          goodsStatus: {},
          goodsType: {}
        },
        // 商品分类集合
        categoryData: [],
        // 品牌集合
        listBrands: [],
        // 勾选集合
        multipleSelection: [],
        total: 0,
        listLoading: false,
        dataList: []
      }
    },
    watch: {
      showFlag(val) {
        if (val) {
          this.resetSearch()
        }
      }
    },
    // 初始化页面
    created() {
      // 获取品牌
      listBrand(this.getEnterpriseId()).then(response => {
        this.listBrands = response.data
      })
      // 获取商品零售类目
      listGoodsCategory(this.getEnterpriseId()).then(response => {
        this.categoryData = response.data
      })
      // 初始化数据字典
      getDictByGroupName('ProjectGoodsDTO').then(response => {
        this.dict = response.data
      })
      this.initList()
    },
    methods: {
      // 获取商品列表
      initList() {
        this.listLoading = true
        this.searchListQuery.warehouseId = this.warehouseId
        this.searchListQuery.goodsSource = this.goodsSource
        this.searchListQuery.enterpriseId = this.getEnterpriseId()
        this.searchListQuery.supplierId = this.getSupplierId()
        this.searchListQuery.oneCategoryId = this.categoryIds.length > 0 ? this.categoryIds[0] : ''
        this.searchListQuery.twoCategoryId = this.categoryIds.length > 1 ? this.categoryIds[1] : ''
        this.searchListQuery.threeCategoryId = this.categoryIds.length > 2 ? this.categoryIds[2] : ''
        pageWarehouseSelectGoods(this.searchListQuery).then(response => {
          if (response.isError) return
          this.total = response.data.total
          this.dataList = response.data.records
          this.listLoading = false
        })
      },
      // 查询事件
      handleSearch() {
        this.searchListQuery.currentPage = 1
        this.initList()
      },
      // 重置事件
      resetSearch() {
        const originData = this.$options.data()
        this.searchListQuery = originData.searchListQuery
        this.categoryIds = originData.categoryIds
        this.initList()
      },
      // 添加商品
      doAddGoods(row) {
        let form
        if (row) {
          form = {
            warehouseId: this.warehouseId,
            goodsId: row.goodsId
          }
        } else {
          if (this.multipleSelection.length <= 0) {
            this.$message({
              message: '请勾选商品',
              type: 'warning'
            })
            return
          }
          form = {
            warehouseId: this.warehouseId,
            goodsIds: this.multipleSelection.map(item => item.goodsId)
          }
        }
        this.listLoading = true
        addGoods(form).then(response => {
          this.listLoading = false
          if (response.isError) return
          this.initList()
          this.$emit('refresh')
          this.$message({
            type: 'success',
            message: '添加成功!'
          })
        })
      },
      // 取消事件
      doCancel() {
        this.$emit('closeDialog')
      },
      // 更改当前页数
      handleCurrentChange(val) {
        this.searchListQuery.currentPage = val
        this.initList()
      },
      // 更改当前页面大小
      handleSizeChange(val) {
        this.searchListQuery.size = val
        this.initList()
      },
      // 勾选事件
      handleSelectionChange(val) {
        this.multipleSelection = val
      }
    }
  }
</script>

<style lang="less" scoped>
  .user-avatar {
    width: 68px;
    height: 68px;
    display: block;
    padding: 1px;
    border: #efefef 1px solid;
  }
</style>
